<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0 ,minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
		<title>花鸟鱼虫特卖网</title>
		<link href="../AmazeUI-2.4.2/assets/css/amazeui.css" rel="stylesheet" type="text/css" />
		<link href="../basic/css/demo.css" rel="stylesheet" type="text/css" />
		<link href="../css/cartstyle.css" rel="stylesheet" type="text/css" />
		<link href="../css/jsstyle.css" rel="stylesheet" type="text/css" />
		<script type="text/javascript" src="../js/address.js"></script>
		<script type="text/javascript" src="../js/common.js"></script>
	</head>

	<body>

		<!--顶部导航条 -->
		<div class="am-container header">
			<ul class="message-l">
				<div class="topMessage">
					<div class="menu-hd" id="indexHeader">
					</div>
				</div>
			</ul>
			<ul class="message-r">
				<div class="topMessage home">
					<div class="menu-hd">
						<a href="index.html" target="_top" class="h">商城首页</a>
					</div>
				</div>
				<div class="topMessage my-shangcheng">
					<div class="menu-hd MyShangcheng">
						<a href="pindex.html" target="_top"><i class="am-icon-user am-icon-fw"></i>个人中心</a>
					</div>
				</div>
			</ul>
		</div>

		<!--悬浮搜索框-->

		<div class="nav white">
			<div class="logo"><img src="../images/logo.png" /></div>
		</div>

		<div class="clear"></div>
		<div class="concent">
			<!--地址 -->
			<div class="paycont">
				<div class="address">
					<div class="control">
						<div class="tc-btn createAddr theme-login am-btn am-btn-danger">填写地址</div>
					</div>
					<div class="clear"></div>
					<ul id="showAddress">
						<li class="user-addresslist defaultAddr">

							<div class="address-left">
								<div class="user DefaultAddr">
									<span class="buy-address-detail">   
                   					<span class="buy-user"> </span>
									<span class="buy-phone"></span>
									</span>
								</div>
								<div class="default-address DefaultAddr">
									<span class="buy-line-title buy-line-title-type">收货地址：</span>
									<span class="buy--address-detail">
										<span class="street"></span>
									</span>
								</div>
							</div>
							<div class="address-right">
								<a href="../person/address.html">
									<span class="am-icon-angle-right am-icon-lg"></span></a>
							</div>
							<div class="clear"></div>

							<div class="new-addr-btn">
								<a href="#" class="hidden">设为默认</a>
								<span class="new-addr-bar hidden">|</span>
								<!--<a href="#">编辑</a>
								<span class="new-addr-bar">|</span>
								<a href="javascript:void(0);" onclick="delClick(this);">删除</a>-->
							</div>

						</li>
					</ul>
					<div class="clear"></div>
				</div>
				<!--物流 -->
				<div class="logistics">
					<h3>选择物流方式</h3>
					<ul class="op_express_delivery_hot">
						<li data-value="圆通" class="OP_LOG_BTN  ">圆通<span></span></li>
                        <li data-value="申通" class="OP_LOG_BTN  ">申通<span></span></li>
                        <li data-value="顺丰" class="OP_LOG_BTN  ">顺丰<span></span></li>
					</ul>
				</div>
				<div class="clear"></div>

				<!--订单 -->
				<div class="concent">
					<div id="payTable">
						<h3>确认订单信息</h3>
						<div class="cart-table-th">
							<div class="wp">

								<div class="th th-item">
									<div class="td-inner">商品信息</div>
								</div>
								<div class="th th-price">
									<div class="td-inner">单价</div>
								</div>
								<div class="th th-amount">
									<div class="td-inner">数量</div>
								</div>
								<div class="th th-sum">
									<div class="td-inner">金额</div>
								</div>
							</div>
						</div>
						<div class="clear"></div>

						<tr class="item-list">
							<div class="bundle  bundle-last">

								<div class="bundle-main">

								</div>
						</tr>
						<div class="clear"></div>
						</div>
					</div>
						<div class="clear"></div>
						<div class="pay-total">
							<!--留言-->
							<div class="order-extra">
								<div class="order-user-info">
									<div id="holyshit257" class="memo">
										<label>买家留言：</label>
										<input style="height: 30px;" id="remark" type="text" title="" placeholder="选填" class="memo-input J_MakePoint c2c-text-default memo-close"/>
										<div class="msg hidden J-msg">
											<p class="error">最多输入500个字符</p>
										</div>
									</div>
								</div>

							</div>
							<!--优惠券 -->
							<div class="buy-agio">
							</div>
							<div class="clear"></div>
						</div>
						

						<!--信息 -->
						<div class="order-go clearfix">
							<div class="pay-confirm clearfix">
								<div class="box">
									<div tabindex="0" id="holyshit267" class="realPay"><em class="t">实付款：</em>
										<span class="price g_price ">
                                    <span>¥</span> <em class="style-large-bold-red " id="J_ActualFee"></em>
										</span>
									</div>

									<div id="holyshit268" class="pay-address">
									</div>
								</div>


								<div id="holyshit269" class="submitOrder">
									<div class="go-btn-wrap">
										<a id="J_Go" onclick="submitOrder()" class="btn-go" tabindex="0" title="点击此按钮，提交订单">提交订单</a>
									</div>
								</div>
								<div class="clear"></div>
							</div>
						</div>
					</div>

					<div class="clear"></div>
				</div>
			</div>
		</div>
		<div class="theme-popover-mask"></div>
		<div class="theme-popover">

			<!--标题 -->
			<div class="am-cf am-padding">
				<div class="am-fl am-cf"><strong class="am-text-danger am-text-lg">新增地址</strong></div>
			</div>
			<hr/>

			<div class="am-u-md-12">
				<form class="am-form am-form-horizontal">

					<div class="am-form-group">
						<label for="user-name" class="am-form-label">收货人</label>
						<div class="am-form-content">
							<input type="text" id="user-name" placeholder="收货人">
						</div>
					</div>

					<div class="am-form-group">
						<label for="user-phone" class="am-form-label">手机号码</label>
						<div class="am-form-content">
							<input id="user-phone" placeholder="手机号必填" type="email">
						</div>
					</div>

					<div class="am-form-group">
						<label for="user-intro" class="am-form-label">详细地址</label>
						<div class="am-form-content">
							<textarea class="" rows="3" id="user-intro" placeholder="输入详细地址"></textarea>
							<small>100字以内写出你的详细地址...</small>
						</div>
					</div>

					<div class="am-form-group theme-poptit">
						<div class="am-u-sm-9 am-u-sm-push-3">
							<div class="am-btn am-btn-danger" onclick="saveAddress()">保存</div>
							<div class="am-btn am-btn-danger close">取消</div>
						</div>
					</div>
				</form>
			</div>

		</div>

		<div class="clear"></div>
		<script>
			function getQueryStringByName(name) {
		        var result = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));
		        if (result == null || result.length < 1) {
		            return "";
		        }
		        return result[1];
		    }
			
			$(function(){
				/*检验用户是否登录*/
					if (getCookie("username") == null || getCookie("userid") == null) {
	                    window.location.href="login.html";
	                    return;
				    }else{
				    	$("#indexHeader").append(`
	                            <a href="#" target="_top" class="h">您好，${getCookie("username")}</a>
	                            <a href="javascript:void(0);" onclick="clearCookie()">退出</a>`
	                    );
				    }
			})
			
			$(function(){
				/*查询优惠券信息*/
                var userid=getCookie("userid");
                $.ajax({
                    type:"get",
                    url:"/user/selectCoupons/"+userid+"/0",
                    async:false,
                    success:function(data){
                        if(data.length<1){
                            console.log("无优惠券");
                        }else{
                            $(".buy-agio").append(`<li class="td td-coupon">
                                    <span class="coupon-title">优惠券</span>
                                    <select data-am-selected id="couponSelect">
                                    </select>
                                </li>`);
                            for(let i=0;i<data.length;i++){
                                $("#couponSelect").append(`
                                    <option value="${data[i].id}" selected>
                                        <div class="c-price">
                                            <strong>￥${data[i].couponValue}</strong>
                                        </div>
                                        <div class="c-limit">
                                                                                                           【无使用门槛】 
                                        </div>
                                    </option>`);
                            }
                        }
                    }
                });
                
                
                var couponValue;
                var couponid=$("#couponSelect").val();
                $.ajax({
                    type:"get",
                    url:"/user/selectCouponById/"+couponid,
                    async:false,
                    success:function(res){
                        couponValue=res.couponValue;
                    }
                })
				
				var cartids=getQueryStringByName("cartids").split(",");
				for(let i=0;i<cartids.length;i++){
					let shopcart;
					let goods;
					
					//请求购物车数据
					$.ajax({
						type:"get",
						url:"/shopcart/info/"+cartids[i],
						async:false,
						success:function(result){
							shopcart=result.data;
						}
					});
					
					//请求商品信息
					$.ajax({
						type:"get",
						url:"/goods/info/"+shopcart.goodsid,
						async:false,
						success:function(result){
							goods=result.data;
						}
					});
					var totalPrice=shopcart.goodsnum*goods.price;
					$(".bundle-main").append(`
						<ul class="item-content clearfix">
						<div class="pay-phone">
							<li class="td td-item">
								<div class="item-pic">
								<input class="item_cartid" type="hidden" value="${shopcart.id}"/>
								<input class="item_goodsid" type="hidden" value="${goods.id}"/>
									<a href="#" class="J_MakePoint">
										<img src="${goods.img1}" style="width:80px;heigth:100px;" class="itempic J_ItemImg" id="itempic"/></a>
								</div>
								<div class="item-info">
									<div class="item-basic-info">
										<a href="#" id="goodsname" class="item-title J_MakePoint" 
										  data-point="tbcart.8.11">${goods.goodsname}</a>
									</div>
								</div>
							</li>
							<li class="td td-info">
								<div class="item-props">
									<span class="sku-line"></span>
								</div>
							</li>
							<li class="td td-price">
								<div class="item-price price-promo-promo">
									<div class="price-content">
										<em class="J_Price price-now" id="price">${goods.price}</em>
										<input class="itemprice" type="text" id="hidePrice" hidden value="${goods.price}"/>
									</div>
								</div>
							</li>
						</div>
						<li class="td td-amount">
							<div class="amount-wrapper ">
								<div class="item-amount ">
									<span class="phone-title">购买数量</span>
									<div class="sl">
										<input class="min am-btn" name="" type="button" value="-" />
										<input id="goodsNum" class="text_box" name="" type="text" value="${shopcart.goodsnum}" style="width:30px;" />
										<input class="add am-btn" name="" type="button" value="+" />
									</div>
								</div>
							</div>
						</li>
						<li class="td td-sum">
							<div class="td-inner">
								<em tabindex="0" id="totalPrice" class="J_ItemSum number">${totalPrice}</em>
							</div>
						</li>
						</ul>`);
						totalMoney(couponValue);
				}
				
				//加
				$(".add").click(function(){
		          var t=$(this).parent().find('input[class*=text_box]');
		          t.val(parseInt(t.val())+1)
		          var itemprice=$(this).parents(".item-content").find(".itemprice").val();
		          var totalprice=(itemprice*t.val()).toFixed(2);
		          $(this).parents(".item-content").find(".J_ItemSum").html(totalprice);
		          totalMoney(couponValue);
		        })
				//减
				$(".min").click(function(){
			         var t=$(this).parent().find('input[class*=text_box]');
			         t.val(parseInt(t.val())-1);
			         if(parseInt(t.val())<1){
			           t.val(1);
			         }
			         var itemprice=$(this).parents(".item-content").find(".itemprice").val();
		             var totalprice=(itemprice*t.val()).toFixed(2);
		             $(this).parents(".item-content").find(".J_ItemSum").html(totalprice);
		             totalMoney(couponValue);
			    })
			})
			
			
			/*保存地址*/
			function saveAddress(){
				var recipient=$("#user-name").val();
				var tel=$("#user-phone").val();
				var address=$("#user-intro").val();
				$('.theme-popover-mask').hide();
				$(".theme-popover").hide();
				$(document.body).css("overflow","");
				$(".buy-user").text(recipient);
				$(".buy-phone").text(tel);
				$(".street").text(address);
			}
			
			/*计算订单总金额*/
			function totalMoney(couponValue){
				var total_money = 0;
        		$(".item-content").each(function(){
        			var itemSum=$(this).find(".J_ItemSum").html();
        			total_money+=Number(itemSum);
        		});
        		if(couponValue!=null){
        			  $("#J_ActualFee").text(total_money-couponValue);
        		}else{
        			  $("#J_ActualFee").text(total_money);
        		}
			}
			
			/*提交订单*/
			function submitOrder(){
				//买家id
				var buyerid=getCookie("userid");
				//收件地址
				var address=$(".street").text();
				//手机号
				var tel=$(".buy-phone").text();
				//收件人
				var recipient=$(".buy-user").text();
				//订单总额
				var totalPrice=$("#totalPrice").text();
				//物流
				var logistics;
				$(".op_express_delivery_hot").find("li").each(function(){
					if($(this).hasClass("selected")){
						logistics=$(this).attr('data-value');
					}
				});
				//备注
				var remark=$("#remark").val();
				var couponid=$("#couponSelect").val();
				
				if(address==''||tel==""||recipient==''){
					alert("请将收货信息填写完整");
					return;
				}
				
				//订单详情数据
				var orderdetails=[];
				$(".item-content").each(function(){
        			var itemSum=$(this).find(".J_ItemSum").html();
        			var goodsid=$(this).find(".item_goodsid").val();
        			var goodsname=$(this).find("#goodsname").html();
        			var goodsnum=$(this).find("#goodsNum").val();
        			var totalprice=$(this).find("#totalPrice").html();
        			var orderdetail={"goodsid":goodsid,"goodsname":goodsname,
        			   "goodsnum":goodsnum,"totalprice":totalprice};
        			orderdetails.push(orderdetail);
        		});
				
				var orderData={"buyerid":buyerid,"status":0,"address":address,
				              "tel":tel,"recipient":recipient,"logistics":logistics,"remark":remark,
				              "orderdetails":orderdetails,"couponid":couponid};
				$.ajax({
					type:"post",
					url:"/orders/insert",
					async:true,
					data:JSON.stringify(orderData),
					contentType: "application/json;charset=utf-8",
					dataType: "json",
					success:function(result){
						if(result.resultCode == 200) {
							//删除购物车数据
							var ids=getQueryStringByName("cartids");
							$.ajax({
								type:"get",
								url:"/shopcart/delete/"+ids,
								success:function(){
									window.location.href="success.html";
								}
							})
						}else{
							alert(result.message);
							return;
						}
					}
				});
			}
		</script>
	</body>

</html>